﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace CommonLibrary.Cookie
{
    public class SingleCookie
    {
        

        /// <summary>
        /// set cookie
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <param name="cookieValue">cookie value</param>
        /// <param name="cookieExpires">cookie expires time</param>
        public static void SetCookie(string cookieName, string cookieValue, CookieExpires cookieExpires)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                HttpContext.Current.Response.Cookies.Remove(cookieName);
            }
            HttpCookie Cookies = new HttpCookie(cookieName);
            Cookies.Value = HttpUtility.UrlEncode(cookieValue);
            if (System.Configuration.ConfigurationManager.AppSettings["PageDomain"] != null)
            {
                Cookies.Domain = System.Configuration.ConfigurationManager.AppSettings["PageDomain"];
            }
            switch (cookieExpires)
            {
                case CookieExpires.Browser:
                    break;
                case CookieExpires.OneHour:
                    Cookies.Expires = DateTime.Now.AddHours(1);
                    break;
                case CookieExpires.OneDay:
                    Cookies.Expires = DateTime.Now.AddDays(1);
                    break;
                case CookieExpires.OneWeek:
                    Cookies.Expires = DateTime.Now.AddDays(7);
                    break;
                case CookieExpires.OneMonth:
                    Cookies.Expires = DateTime.Now.AddMonths(1);
                    break;
                case CookieExpires.OneYear:
                    Cookies.Expires = DateTime.Now.AddYears(1);
                    break;
            }
            HttpContext.Current.Response.Cookies.Add(Cookies);
        }



        /// <summary>
        /// set Encrypt cookie
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <param name="cookieValue">cookie value</param>
        /// <param name="cookieExpires">cookie expires time</param>
        public static void SetEncryptCookie(string cookieName, string cookieValue, CookieExpires cookieExpires)
        {
            SetCookie(cookieName, cookieValue.ClEncryptString(), cookieExpires);
        }



        /// <summary>
        /// set Encrypt cookie
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <param name="cookieValue">cookie value</param>
        /// <param name="cookieExpires">cookie expires time</param>
        public static void SetEncryptCookie(string cookieName, string cookieValue, string encryptKey, CookieExpires cookieExpires)
        {
            SetCookie(cookieName,cookieValue.ClEncryptString(encryptKey) , cookieExpires);
        }





        /// <summary>
        /// get cookie value
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <returns>cookie value</returns>
        public static string GetCookieValue(string cookieName)
        {
            string cookieValue = "";
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                cookieValue = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[cookieName].Value);
            }
            return cookieValue;
        }



        /// <summary>
        /// get encrypt cookie value
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <returns>cookie value</returns>
        public static string GetEncryptCookieValue(string cookieName)
        {
            string cookieValue = "";
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                cookieValue = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[cookieName].Value.ClDecryptString());
            }
            return cookieValue;
        }




        /// <summary>
        /// get encrypt cookie value
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <returns>cookie value</returns>
        public static string GetEncryptCookieValue(string cookieName, string decryptKey)
        {
            string cookieValue = "";
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                cookieValue = HttpUtility.UrlDecode(  HttpContext.Current.Request.Cookies[cookieName].Value.ClDecryptString(decryptKey));
            }
            return cookieValue;
        }




        /// <summary>
        /// remover cookie
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        public static void RemoveCookie(string cookieName)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                HttpContext.Current.Response.Cookies.Remove(cookieName);
            }
            HttpCookie Cookies = new HttpCookie(cookieName);
            Cookies.Value = "novalue";
            if (System.Configuration.ConfigurationManager.AppSettings["PageDomain"] != null)
            {
                Cookies.Domain = System.Configuration.ConfigurationManager.AppSettings["PageDomain"];
            }
            Cookies.Expires = DateTime.Now.AddDays(-7);
            HttpContext.Current.Response.Cookies.Add(Cookies);
        }
    }
}
